package com.sam.im.sms.index.adapters;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sam.im.sms.R;
import com.sam.im.sms.beans.ThreadDetailBean;
import com.sam.im.sms.entity.InterceptEntity;
import com.sam.im.sms.util.DensityUtil;
import com.sam.im.sms.util.TimeFormat;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述
 * liuyucheng
 * Created By 刘育成 on 2019/6/10.
 */
public class InterceptDetailsAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private List<ThreadDetailBean> mData;
    ThreadDetailBean threadDetailBean;
    int layoutId;

    public InterceptDetailsAdapter(Context context,List<ThreadDetailBean> list) {
        mContext = context;
        mData = list;
    }
    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = null;
        if (viewType == 123) {
            //type 1 对方
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.hei_item, parent, false);
        } else if (layoutId == R.layout.me_item) {
            //type 2 自己
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.me_item, parent, false);
        }
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ViewHolder) holder).setPosition(position);
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        threadDetailBean = mData.get(position);
        layoutId = threadDetailBean.getLayoutId();
        if (layoutId == R.layout.hei_item) {
            return 123;
        } else {
            return 456;
        }
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView content;
        private int position;
        private TextView txtDateTime;
        private RelativeLayout rlTitle;
        private View vLine;
        private TextView formatTime;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            content = (TextView) itemView.findViewById(R.id.content);
            rlTitle = (RelativeLayout) itemView.findViewById(R.id.rl_title);
            vLine = itemView.findViewById(R.id.v_line);
            txtDateTime = (TextView) itemView.findViewById(R.id.txt_date_time);
            formatTime = itemView.findViewById(R.id.tv_time);
        }

        public void setPosition(int position) {
            this.position = position;
            content.setText(threadDetailBean.getContent());
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) vLine.getLayoutParams();
            if (position == 0) {
                if (mData.size() >= 2) {
                    if (!threadDetailBean.getDate().equals(mData.get(position + 1).getDate())) {
                        content.setBackgroundResource(R.drawable.message_sys_bgfirst);
                        layoutParams.setMargins(DensityUtil.dip2px(vLine.getContext(), 20), DensityUtil.dip2px(vLine.getContext(), 15), 0, 0);
                    } else {
                        content.setBackgroundResource(R.drawable.message_sys_bgtop);
                        layoutParams.setMargins(DensityUtil.dip2px(vLine.getContext(), 20), DensityUtil.dip2px(vLine.getContext(), 15), 0, 0);
                    }
                } else {
                    content.setBackgroundResource(R.drawable.message_sys_bgfirst);
                    layoutParams.setMargins(DensityUtil.dip2px(vLine.getContext(), 20), DensityUtil.dip2px(vLine.getContext(), 15), 0, 0);
                }
                rlTitle.setVisibility(View.VISIBLE);
                txtDateTime.setText(TimeFormat.stampToDate(threadDetailBean.getDate()));
                formatTime.setText(TimeFormat.stampToTime(threadDetailBean.getDate()));
                //代码设置是px
                layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.rl_title);
                layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.txt_date_title);
            } else if (position < mData.size() - 1) {
                if (threadDetailBean.getDate().equals(mData.get(position - 1).getDate())) {
                    if (threadDetailBean.getDate().equals(mData.get(position + 1).getDate())) {
                        rlTitle.setVisibility(View.GONE);
                        layoutParams.setMargins(DensityUtil.dip2px(vLine.getContext(), 20), DensityUtil.dip2px(vLine.getContext(), 0), 0, 0);
                        content.setBackgroundResource(R.drawable.message_sys_bgcenter);
                        layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.txt_date_title);
                        layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.txt_date_title);
                    } else {
                        rlTitle.setVisibility(View.GONE);
                        content.setBackgroundResource(R.drawable.message_sys_bgbot);
                        layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.txt_date_title);
                        layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.txt_date_title);
                    }

                } else {
                    if (!threadDetailBean.getDate().equals(mData.get(position + 1).getDate())) {
                        content.setBackgroundResource(R.drawable.message_sys_bgfirst);
                    } else {
                        content.setBackgroundResource(R.drawable.message_sys_bgtop);
                    }
                    layoutParams.setMargins(DensityUtil.dip2px(vLine.getContext(), 20), DensityUtil.dip2px(vLine.getContext(), 0), 0, 0);
                    rlTitle.setVisibility(View.VISIBLE);

                    txtDateTime.setText(TimeFormat.stampToDate(threadDetailBean.getDate()));
                    formatTime.setText(TimeFormat.stampToTime(threadDetailBean.getDate()));
                    layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.rl_title);
                    layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.txt_date_title);
                }

            } else {
                if (!threadDetailBean.getDate().equals(mData.get(position - 1).getDate())) {
                    content.setBackgroundResource(R.drawable.message_sys_bgfirst);
                    rlTitle.setVisibility(View.VISIBLE);
                    txtDateTime.setText(TimeFormat.stampToDate(threadDetailBean.getDate()));
                    formatTime.setText(TimeFormat.stampToTime(threadDetailBean.getDate()));
                    layoutParams.setMargins(DensityUtil.dip2px(vLine.getContext(), 20), DensityUtil.dip2px(vLine.getContext(), 0), 0, 0);
                    layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.rl_title);
                    layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.txt_date_title);
                } else {
                    rlTitle.setVisibility(View.GONE);
                    content.setBackgroundResource(R.drawable.message_sys_bgbot);
                    txtDateTime.setText(TimeFormat.stampToDate(threadDetailBean.getDate()));
                    formatTime.setText(TimeFormat.stampToTime(threadDetailBean.getDate()));
                    layoutParams.setMargins(DensityUtil.dip2px(vLine.getContext(), 20), DensityUtil.dip2px(vLine.getContext(), 0), 0, 0);
                    layoutParams.addRule(RelativeLayout.ALIGN_TOP, R.id.txt_date_title);
                    layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, R.id.txt_date_title);
                }

            }
            vLine.setLayoutParams(layoutParams);
            content.setText(threadDetailBean.getContent());
        }
    }
}
